<?php
	include('header.php');	// 包含页头文件
	// start 检查并发布评论
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		// start 检测验证码是否正确，如果错误跳转会主页
		if ($_POST['yanzhengma_comment']!=$_SESSION['authnum_session']) {
		?> 
		<meta http-equiv="Refresh" content="3;URL=<?php echo ($_SERVER['PHP_SELF'].'?aid='.$_POST['article_id']);?>" />
		<div style="text-align:center;padding:50px;border-style:dashed;border-width:1px;">
			验证码错误，系统将自动跳转到帖子页面<br /><br />
			<a href="<?php echo $_SERVER['PHP_SELF'].'?aid='.$_POST['article_id'];?>">如果您的浏览器没有自动跳转,请点击这里回到帖子页</a>
		</div>
		<?php
			exit;
		}
		// end
		// start 插入评论到数据库
		$query_post_comment = "insert into `comment` (`articles_id`, `content`, `post_time`, `nickname`) values (".$_POST['article_id'].", '".addslashes($_POST['content_comment'])."', now(), '".addslashes($_POST['nickname_comment'])."')";
		$mysql_connection->query($query_post_comment);

		// 发送通知邮件给博客作者
		if ($setting['email_new_comment']=='true') {
			$to      = $setting['email_root'];
			$subject = $_POST['nickname_comment'].'在您的博客留下了新评论';
			$message = '您的博客有新评论：'.$_POST['content_comment'];
			$headers = 'From: root@awanglei.3322.org' . "\r\n" .
				'Reply-To: 272202193@qq.com' . "\r\n" .
				'X-Mailer: PHP/' . phpversion();

			mail($to, $subject, $message, $headers);
		}
		// 发送通知邮件给博客作者 END
	?>
	<meta http-equiv="Refresh" content="3;URL=<?php echo ($_SERVER['PHP_SELF'].'?aid='.$_POST['article_id']);?>" />
	<div style="text-align:center;padding:50px;border-style:dashed;border-width:1px;">
		评论发表成功，系统将自动跳转到帖子页面<br /><br />
		<a href="<?php echo $_SERVER['PHP_SELF'].'?aid='.$_POST['article_id'];?>">如果您的浏览器没有自动跳转,请点击这里回到帖子页</a>
	</div>
	<?php
		exit;
		// end
	}
?> 
<div id="content">
	<div id="article_content">
		<?php
			// start 从url或者article_id并查询显示帖子。
			// start 查询 articles 表的帖子内容和标题，发表时间
			$article_id = $_GET['aid'];
			$query_article = "select * from articles where id=".$article_id;
			$result_article = $mysql_connection->query($query_article);
			$this_result_article = $result_article->fetch_assoc();
			// end 查询 articles 表的帖子内容和标题，发表时间

			// start 查询分类信息和支持反对数量
			$category_array = get_category($article_id, $mysql_connection);
			$count_zhichi_fandui = get_count_zhichi_fandui($article_id, $mysql_connection);
			// end 查询分类信息和支持反对数量
		?> 
		<div class="article">
			<div class="article_title"><strong><?php echo $this_result_article['title']; ?></strong></div>
			<div class="article_content"><?php echo $this_result_article['content']; ?></div>
			<div class="article_footer">
				<img src="images/date_time.png"></img>
				发表时间：<?php echo $this_result_article['post_time'];?> |&nbsp;
				<img src="images/tag.png"></img>&nbsp;分类：
				<?php
					foreach ($category_array as $category_id => $category_name) {
						echo '<a href="index.php?cat='.$category_id.'"/>'.$category_name.'</a>&nbsp;';
					}
				?>|
				<a href="javascript:;" onclick="add_zhichi_fandui('<?php echo $_SERVER['REMOTE_ADDR'];?>','zhichi',<?php echo $article_id;?>);"><span id="zhichi"><?php echo $count_zhichi_fandui['zhichi'];?></span>&nbsp;支持<img height="20px" src="images/zhichi.png" /></a>
				&nbsp;|&nbsp;
				<a href="javascript:;" onclick="add_zhichi_fandui('<?php echo $_SERVER['REMOTE_ADDR'];?>','fandui',<?php echo $article_id;?>);"><span id="fandui"><?php echo $count_zhichi_fandui['fandui'];?></span>&nbsp;反对<img height="20px" src="images/fandui.png" /></a>|
				<span id="isnot_toupiao"></span>
			</div>
		</div>
		<!-- end 从 url 获取 article_id 并查询显示帖子 -->
		<div id="next_pre_page">
			<div id="next_page">
				<?php
					// start 根据发布时间查询上一篇帖子和下一篇帖子
					$query_pre_article = "select * from `articles` where `post_time` < '".$this_result_article['post_time']."' order by `post_time` desc limit 1";
					$result_pre_article = $mysql_connection->query($query_pre_article);
					$pre_article = $result_pre_article->fetch_assoc();
					if (isset($pre_article['id'])) {
					?>
					<a href="article.php?aid=<?php echo $pre_article['id']; ?>"><?php echo $pre_article['title']; ?>&nbsp;<img src="images/right.png" /></a>
					<?php
					}
				?>
			</div>
			<div id="pre_page">
				<?php
					$query_next_article = "select * from `articles` where `post_time` > '".$this_result_article['post_time']."' order by `post_time` asc limit 1";
					$result_next_article = $mysql_connection->query($query_next_article);
					$next_article = $result_next_article->fetch_assoc();
					// end
					if (isset($next_article['id'])) {
					?> 
					<a href="article.php?aid=<?php echo $next_article['id']; ?>"><img src="images/left.png" />&nbsp;<?php echo $next_article['title']; ?></a>
					<?php
					}
				?>
			</div>
		</div>
		<div id="comments">
			<?php
				// start 根据帖子ID查询帖子下面的评论
				$query_comment = "select * from `comment` where `articles_id`=".$article_id;
				$result_comment = $mysql_connection->query($query_comment);
				$num_comment = $result_comment->num_rows;
				// start 显示评论数量
				if ($num_comment>0) {
				?> 
				<div id="show_count_comment">
					现在有<?php echo $num_comment; ?>篇评论
				</div>
				<?php
				} else {
				?>
				<div id="show_count_comment">
					暂时没有评论
				</div>
				<?php
				}
				// end
				for ($i=0; $i<$num_comment; $i+=1) {
					$comment_one = $result_comment->fetch_assoc();
					if ($i % 2 == 0) {
						$comment_class_name = 'comment';
					} else {
						$comment_class_name = 'comment_b';
					}
				?> 
				<div class="<?php echo $comment_class_name; ?>">
					<a name="<?php echo $comment_one['id'];?>-comment"></a>
					<?php echo $comment_one['nickname']; ?>&nbsp;&nbsp;写道：<br />
					<div class="comment_content"><?php echo $comment_one['content']; ?></div>
					<div class="comment_time">发表时间：<?php echo $comment_one['post_time'];?>|
					<a href="#<?php echo $comment_one['id'];?>-comment">#<?php echo $i+1;?>楼</a></div>
				</div>
				<?php
				}
				// end
			?>
		</div>
		<!-- start 发布评论表单 -->
		<div id="comment_poster">
			<br /><hr />
			发表评论
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
				昵称：<input type="text" class="textwenbenkuang" name="nickname_comment" id="nickname_comment" /><br />
				验证码：<input type="text" class="textwenbenkuang" name="yanzhengma_comment" id="yanzhengma_comment" />
				<img src="yanzhengma.php" />
				<textarea name="content_comment" style="width:100%" cols="50" rows="5" id="content_comment"></textarea><br /><br />
				<input type="hidden" value="<?php echo $article_id; ?>" id="article_id" name="article_id" />
				<div id="submit_div"><input id="submit_article" type="submit" value="提 交" /></div>
			</form>
		</div>
		<!-- end 发布评论表单 -->
	</div>
	<?php include('sidebar.php'); ?> 
</div>
<?php include('footer.php');?> 
